#include "Utils.h"
#include "string.h"
#include "time.h"
#include "stdio.h"

void xcrypt(char* inputByte, unsigned long inputLength, char* encriptedByte, char* key){

	unsigned long keyLength = strlen(key);

	for(int i = 0; i < inputLength; i++){
		int keyIndex = i%keyLength;
		char sourceChar = inputByte[i];

		sourceChar = sourceChar^key[keyIndex];
		encriptedByte[i] = sourceChar;
	}
}

char* getCurrentTimeStringForFileName(){
	time_t t = time(0);   // get time now
	struct tm * now = localtime( & t );

	char timeString[32] = {0};
	char tempString[16] = {0};

	sprintf(tempString, "%d.", now->tm_year + 1900);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d.", now->tm_mon + 1);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d-", now->tm_mday);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d.", now->tm_hour);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d.", now->tm_min);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d.", now->tm_sec);
	strcat(timeString, tempString);

	return timeString;
}

char* getCurrentTimeString(){
	time_t t = time(0);   // get time now
	struct tm * now = localtime( & t );

	char timeString[32] = {0};
	char tempString[16] = {0};

	sprintf(tempString, "%d-", now->tm_year + 1900);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d-", now->tm_mon + 1);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d ", now->tm_mday);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d:", now->tm_hour);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d:", now->tm_min);
	strcat(timeString, tempString);

	sprintf(tempString, "%02d", now->tm_sec);
	strcat(timeString, tempString);

	return timeString;
}

CCString* Utils::getTimeString(int timeInt) {
    int hour = timeInt/3600;
    int minute = (timeInt%3600)/60;
    int second = timeInt - hour*3600 - minute*60;
    CCString* str = CCString::createWithFormat("%.2d:%.2d:%.2d", hour, minute, second);
    return str;
}